package com.sany.taskmanager.ui.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;

import androidx.annotation.NonNull;

import com.sany.taskmanager.R;
import com.sany.taskmanager.bean.ColorBean;
import com.sany.taskmanager.databinding.DialogColorBinding;
import com.sany.taskmanager.http.IHttpCallBack;
import com.sany.taskmanager.ui.activity.BaseActivity;
import com.sany.taskmanager.ui.adapter.ColorAdapter;
import com.sany.taskmanager.util.ToastUtil;

import java.util.List;

/**
 * 大臂颜色选择弹框
 */
public class ColorDialog extends CommonDialog<DialogColorBinding> {
    private ColorAdapter mAdapter;
    private List<ColorBean> mColorList;
    private ColorAdapter mSanyAdapter;
    private List<ColorBean> mSanyColorList;
    private OnSelectedColorListener mOnSelectedColorListener;

    public void setOnSelectedColorListener(OnSelectedColorListener onSelectedColorListener) {
        mOnSelectedColorListener = onSelectedColorListener;
    }

    public ColorDialog(@NonNull Context context) {
        super(context, context.getString(R.string.title_color_select));
    }

    @Override
    protected void initData() {
        mAdapter = new ColorAdapter(mContext);
        vBinding.rvColor.setAdapter(mAdapter);
        mSanyAdapter = new ColorAdapter(mContext);
        vBinding.rvSanyColor.setAdapter(mSanyAdapter);
        vBinding.etColorCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 4) {
                    initColorInfo();
                }
            }
        });
        initSanyColor();
    }

    /**
     * 获取三一标配颜色
     */
    private void initSanyColor() {
        ((BaseActivity<?>) mContext).showLoading();
        ((BaseActivity<?>) mContext).compositeDisposable.add(((BaseActivity<?>) mContext).taskViewModel.getSanyColorList(new IHttpCallBack<List<ColorBean>>() {
            @Override
            public void onSuccess(List<ColorBean> colorList) {
                ((BaseActivity<?>) mContext).hideLoading();
                if (colorList == null || colorList.isEmpty()) {
                    ToastUtil.showToast(mContext, mContext.getString(R.string.tip_get_color_failed));
                    return;
                }
                mSanyColorList = colorList;
                refreshSanyList();
            }

            @Override
            public void onFailed(String message) {
                ((BaseActivity<?>) mContext).hideLoading();
                ToastUtil.showToast(mContext, message);
            }
        }));
    }


    private void refreshSanyList() {
        mSanyAdapter.setList(mSanyColorList, colorBean -> {
            if (mOnSelectedColorListener != null) {
                mOnSelectedColorListener.getColorBean(colorBean);
            }
            cancel();
        });
    }

    private void refreshList() {
        mAdapter.setList(mColorList, colorBean -> {
            if (mOnSelectedColorListener != null) {
                mOnSelectedColorListener.getColorBean(colorBean);
            }
            cancel();
        });
    }

    /**
     * 初始化颜色列表
     */
    private void initColorInfo() {
        ((BaseActivity<?>) mContext).showLoading();
        ((BaseActivity<?>) mContext).compositeDisposable.add(((BaseActivity<?>) mContext).taskViewModel.getColorListByCode(vBinding.etColorCode.getText().toString(), new IHttpCallBack<List<ColorBean>>() {
            @Override
            public void onSuccess(List<ColorBean> colorList) {
                ((BaseActivity<?>) mContext).hideLoading();
                if (colorList == null || colorList.isEmpty()) {
                    ToastUtil.showToast(mContext, mContext.getString(R.string.tip_get_color_failed));
                    return;
                }
                mColorList = colorList;
                refreshList();
            }

            @Override
            public void onFailed(String message) {
                ((BaseActivity<?>) mContext).hideLoading();
                ToastUtil.showToast(mContext, message);
            }
        }));
    }

    @Override
    protected boolean confirm() {
        return true;
    }

    @Override
    public boolean showOnlyCancelButton() {
        return true;
    }

    public interface OnSelectedColorListener {
        void getColorBean(ColorBean colorBean);
    }

}
